package com.xy.novelweb.config;

import com.xy.novelweb.interceptor.BookShelfInterceptor;
import com.xy.novelweb.interceptor.NovelReadInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class MySpringMVCConfiguration implements WebMvcConfigurer {
		@Bean //<bean>
		public BookShelfInterceptor getBookShelfInterceptor() {
				BookShelfInterceptor myInterceptor = new BookShelfInterceptor();
				return myInterceptor;
		}

		@Bean
		public NovelReadInterceptor getNovelReadInterceptor() {
				return new NovelReadInterceptor();
		}

		//拦截器注册
		@Override
		public void addInterceptors(InterceptorRegistry registry) {
				//拦截访问书架的链接
				registry.addInterceptor(getBookShelfInterceptor()).addPathPatterns("/reader/personshelf");
				//拦截阅读的请求
				registry.addInterceptor(getNovelReadInterceptor()).addPathPatterns("/novel/readbycatlog");
		}

		//配置视图控制器
		@Override
		/*重写addViewControllers实现*/
		public void addViewControllers(ViewControllerRegistry registry) {
				registry.addViewController("/xuanhuan").setViewName("xuanhuan");
				registry.addViewController("/kehuan").setViewName("kehuan");
				registry.addViewController("/lishi").setViewName("lishi");
				registry.addViewController("/dushi").setViewName("dushi");
				registry.addViewController("/wuxia").setViewName("wuxia");
				registry.addViewController("/wangyou").setViewName("wangyou");
				registry.addViewController("/rank").setViewName("rank");
				registry.addViewController("/classification").setViewName("classification");
				registry.addViewController("/novel/book/*").setViewName("book");
				registry.addViewController("/read/*/*").setViewName("book_read");
		}

		/*配置路径映射
		 * */
		@Override
		public void addResourceHandlers(ResourceHandlerRegistry registry) {
				//  /home/file/**为前端URL访问路径  后面 file:xxxx为本地磁盘映射
				registry.addResourceHandler("/picture/**").addResourceLocations("file:D://JavaFileBox/picture/");
				registry.addResourceHandler("/user-photo/**").addResourceLocations("file:D://JavaFileBox/user-photo/");
				registry.addResourceHandler("/auth-photo/**").addResourceLocations("file:D://JavaFileBox/auth-photo/");
		}
}
